package com.xique.park.service.command.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.constant.HttpStatus;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.utils.command.CommandResultUtil;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.api.IParkCarMngTypeCommandApi;
import com.xique.park.bean.domain.ParkCarMngtype;
import com.xique.park.bean.domain.ParkCarMngtypeLane;
import com.xique.park.bean.domain.ParkDeviceNew;
import com.xique.park.bean.request.web.mngType.MngTypeDeleteRequest;
import com.xique.park.constant.CommandConstant;
import com.xique.park.constant.ParkApiName;
import com.xique.park.service.command.service.IParkCarMngTypeCommandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 车辆管理类型命令下发
 *
 * @author: xique
 */
@Service
public class ParkCarMngTypeCommandServiceImpl extends CommandCommServiceImpl implements IParkCarMngTypeCommandService {

    @Autowired
    private IParkCarMngTypeCommandApi parkCarMngTypeCommandApi;

    @Override
    public AjaxResult downMngType(Long parkId, String sn, List<ParkCarMngtype> list) {
        JSONObject dataObj = new JSONObject();
        dataObj.put(CommandConstant.LIST, list);
        List<Long> recIdList = list.stream().map(ParkCarMngtype::getId).collect(Collectors.toList());
        dataObj.put(CommandConstant.REC_IDS, recIdList);

        JSONObject obj = getBaseObj(parkId, sn, ParkApiName.COMMAND_MNG_TYPE_DOWNLOAD, ParkApiName.VER_2_0);
        obj.put(CommandConstant.DATA, dataObj);

        @SuppressWarnings("unchecked")
        Map<String, Object> params = JSONObject.toJavaObject(obj, Map.class);

        AjaxResult commandResult = parkCarMngTypeCommandApi.downMngType(params);
        return CommandResultUtil.handleCommandResult(commandResult, "");
    }

    @Override
    public AjaxResult downMngType(Long parkId, List<ParkCarMngtype> list, String reminderMsg) {
        if (null != parkId) {
            List<String> devCodes = getMainDeviceCodesByParkId(parkId);
            if (CollectionUtil.isNotEmpty(devCodes)) {
                boolean commandResult = true;
                StringBuilder errMsg = new StringBuilder();
                for (String devCode : devCodes) {
                    if (StringUtils.isNotEmpty(devCode)) {
                        AjaxResult result = downMngType(parkId, devCode, list);
                        if (result.get(AjaxResult.CODE_TAG).equals(HttpStatus.ERROR)) {
                            commandResult = false;
                            errMsg.append(result.get(AjaxResult.MSG_TAG)).append("，");
                        }
                    }
                }
                if (commandResult) {
                    return AjaxResult.success(reminderMsg + "下发成功");
                } else {
                    return AjaxResult.commandError(reminderMsg + "下发失败，请重新下发，错误原因：" + errMsg);
                }
            } else {
                return AjaxResult.success(reminderMsg + "车场没有添加主相机，无需下发");
            }
        } else {
            return AjaxResult.commandError(reminderMsg + "下发失败，车场参数不能为空");
        }
    }

    @Override
    public AjaxResult delMngType(String sn, MngTypeDeleteRequest request) {
        Long[] ids = request.getIds();
        List<ParkCarMngtype> list = new ArrayList<>(ids.length);
        ParkCarMngtype mngtype = new ParkCarMngtype();
        for (Long id : ids) {
            mngtype.setId(id);
            list.add(mngtype);
        }

        JSONObject obj = getBaseObj(SecurityUtils.getParkId(), sn, ParkApiName.COMMAND_MNG_TYPE_DEL, ParkApiName.VER_1_0);
        obj.put(CommandConstant.DATA, JSONArray.toJSON(list));

        @SuppressWarnings("unchecked")
        Map<String, Object> params = JSONObject.toJavaObject(obj, Map.class);

        AjaxResult commandResult = parkCarMngTypeCommandApi.delMngType(params);
        return CommandResultUtil.handleCommandResult(commandResult, "");
    }

    @Override
    public AjaxResult delMngType(Long parkId, MngTypeDeleteRequest request, String reminderMsg) {
        if (null != parkId) {
            List<String> devCodes = getMainDeviceCodesByParkId(parkId);
            if (CollectionUtil.isNotEmpty(devCodes)) {
                boolean commandResult = true;
                StringBuilder errMsg = new StringBuilder();
                for (String devCode : devCodes) {
                    if (StringUtils.isNotEmpty(devCode)) {
                        AjaxResult result = delMngType(devCode, request);
                        if (result.get(AjaxResult.CODE_TAG).equals(HttpStatus.ERROR)) {
                            commandResult = false;
                            errMsg.append(result.get(AjaxResult.MSG_TAG)).append("，");
                        }
                    }
                }
                if (commandResult) {
                    return AjaxResult.success(reminderMsg + "下发成功");
                } else {
                    return AjaxResult.commandError(reminderMsg + "下发失败，请重新下发，错误原因：" + errMsg);
                }
            } else {
                return AjaxResult.success(reminderMsg + "车场没有添加主相机，无需下发");
            }
        } else {
            return AjaxResult.commandError(reminderMsg + "下发失败，车场参数不能为空");
        }
    }

    @Override
    public AjaxResult downMngTypePassAuth(Long parkId, String sn, List<ParkCarMngtypeLane> list) {
        JSONObject dataObj = new JSONObject();
        dataObj.put(CommandConstant.LIST, list);
        List<Long> recIdList = list.stream().map(ParkCarMngtypeLane::getId).collect(Collectors.toList());
        dataObj.put(CommandConstant.REC_IDS, recIdList);

        JSONObject obj = getBaseObj(parkId, sn, ParkApiName.COMMAND_PASS_AUTH_DOWNLOAD, ParkApiName.VER_2_0);
        obj.put(CommandConstant.DATA, dataObj);

        @SuppressWarnings("unchecked")
        Map<String, Object> params = JSONObject.toJavaObject(obj, Map.class);

        return parkCarMngTypeCommandApi.downMngTypePassAuth(params);
    }

    @Override
    public AjaxResult downMngTypePassAuth(Long parkId, List<ParkCarMngtypeLane> list, String reminderMsg) {
        if (null != parkId) {
            List<ParkDeviceNew> parkDeviceNewList = getMainDevListByParkId(parkId);
            if (CollectionUtil.isNotEmpty(parkDeviceNewList)) {
                StringBuilder resultMsg = new StringBuilder(reminderMsg).append("</br>");
                for (ParkDeviceNew parkDeviceNew : parkDeviceNewList) {
                    if (StringUtils.isNotEmpty(parkDeviceNew.getCode())) {
                        AjaxResult result = downMngTypePassAuth(parkId, parkDeviceNew.getCode(), list);
                        if (null != result) {
                            if (null != result.get("message")) {
                                String message = (String) result.get("message");
                                resultMsg.append(parkDeviceNew.getName()).append(message).append("</br>");
                            }
                        }
                    }
                }
                return AjaxResult.success(resultMsg.toString());
            } else {
                return AjaxResult.success(reminderMsg + "车场没有添加主相机，无需下发");
            }
        } else {
            return AjaxResult.commandError(reminderMsg + "下发失败，车场参数不能为空");
        }
    }

    @Override
    public JSONObject getCommandStatus(Long parkId, List<Long> recIdList) {
        return getCommandStatus(ParkCarMngtype.class.getSimpleName(), parkId, recIdList);
    }

    @Override
    public JSONObject getPassAuthCommandStatus(Long laneId, Long recId) {
        return getCommandStatus(ParkCarMngtypeLane.class.getSimpleName(), laneId, recId);
    }
}
